// pages/myCoupons/myCoupons.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options.orderProductId)
    this.setData({
      navH: app.globalData.navHeight,
      type:options.type,
      productId:options.productId
    });
    wx.request({
      url: app.globalData.url + 'goAfterSaleCente', 
      data: {
        orderProductId: options.orderProductId,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data.data)
        that.setData({
          orderProduct: res.data.data,
        })
      },

    })
    wx.request({
      url: app.globalData.url + 'defaultAddress', //
      data: {
        userId: wx.getStorageSync("userId"),
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data.data)
        that.setData({
          username: res.data.data.username,
          telephone: res.data.data.telephone,
          wholeAddress: res.data.data.wholeAddress,
          addrId: res.data.data.addrId,
        })
      },

    })
    this.goOut();
  },
  goOut: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + 'getWareHouseByProduct',
      method: 'GET',
      data: {
        productId: that.data.productId,
      },
      success: function (res) {
        that.setData({
          linker: res.data.data.linker,
          telephone1: res.data.data.telephone,
          address: res.data.data.address,
        })
      }
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  // 点击tab切换view
  city: function (e) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    const pages = getCurrentPages()
    const currPage = pages[pages.length - 1] 
  },
  chooseAddress: function () {
    wx.navigateTo({
      url: '../addressList/addressList'
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  submit: function (e) {
    var that = this;
    
    var content = "";
    if (that.data.type == 1) {
      content="确定要发起退款申请吗？"
    } else {
      content = "确定要发起换货申请吗？"
    } 
    
    wx.showModal({
      title: '提示',
      content: content,
      success: function (sm) {
        if (sm.confirm) {
          var  adId = 0;
          if (e.currentTarget.dataset.adid != undefined){
            adId = e.currentTarget.dataset.adid;
          }
          wx.request({
            url: app.globalData.url + 'userAppliesForReturnAndExchange',
            data: {
              orderProductId: e.currentTarget.dataset.opid,
              type: e.currentTarget.dataset.type,
              addressId: adId,
              applyCause: that.data.concent,
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log(res)
              if(res.data.result ==1){
                wx.requestSubscribeMessage({
                  tmplIds: ['m8BOs2XGfzGKMFsA1Jeh9nwlgIi6CqdlZOmD5Ta18eY'],
                  success(res) {
                    console.info(res);
                    'https%3a%2f%2ftest.zhijiapp.com%2fWeChatMessageSubscription'
                  }
                })
                wx.navigateBack({ // 返回首页
                  delta: 3,
                })
              }else{
                wx.showToast({
                  title: ''+res.data.errmsg,
                  icon: "none"
                })
              }
              
            },

          })
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    


  },
  //失去焦点时获取里面评论内容
  bindTextAreaBlur: function (e) {
    console.log(e.detail.value)
    this.setData({
      concent: e.detail.value,
    })
  },
  navBack: function () {
    wx.navigateBack({

    })
  }
})